//
//  GroupedTableController.m
//  MyBus
//
//  Created by Christopher Anderson on 25/01/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GroupedTableController.h"
#import "MenuCellController.h"




@implementation GroupedTableController

@synthesize menuGroups;

- (void) constructMenuGroups {
	self.menuGroups = [NSArray arrayWithObject:[NSArray array]];
}

- (void) updateMenuGroups {
	[self clearMenuGroups];
	[self constructMenuGroups];
	//[self.tableView reloadData];	
}

- (void) clearMenuGroups {
	[menuGroups release];
	menuGroups = nil;
}

- (void) checkMenuGroups {
	if (!menuGroups) {
		[self constructMenuGroups];
	}	
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	[self checkMenuGroups];
	return [menuGroups count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	[self checkMenuGroups];
	// We should refactor here, we are using the rows to store their group heading. We should
	// create another class that contains the groups heading and row controllers. 
	// For now we pick the first row in the section....
	// Don't think the above applies any more....
	return [[menuGroups objectAtIndex:section] tableView:tableView titleForHeaderInSection:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	[self checkMenuGroups];
	return [[menuGroups objectAtIndex:section]
			tableView:tableView numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];
	return [[menuGroups objectAtIndex:indexPath.section]
			tableView:tableView
			cellForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];	
	NSObject<MenuCellController> *cellData = [menuGroups objectAtIndex:indexPath.section];
	
	[cellData tableView:tableView didSelectRowAtIndexPath:indexPath];
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if ([tableView isEditing]) {
		[self setEditing:NO animated:YES];
	}
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];
	return [[menuGroups objectAtIndex:indexPath.section] tableView:tableView accessoryTypeForRowWithIndexPath:indexPath];
}

/*- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];
	return [[menuGroups objectAtIndex:indexPath.section] tableView:tableView heightForRowAtIndexPath:indexPath];
}*/


- (void)tableView:(UITableView *)tableView  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];	
	NSObject<MenuCellController> *cellData = [menuGroups objectAtIndex:indexPath.section];
	[cellData tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	[self checkMenuGroups];
	return [[menuGroups objectAtIndex:indexPath.section] tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (void)didReceiveMemoryWarning {
	[self clearMenuGroups];
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


@end
